package com.mahjong.server.protocolHandler.player;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.mahjong.server.common.Common;
import com.mahjong.server.mina.protocol.AbsMessageProtocol;
import com.mahjong.server.mina.protocol.DataBuf;
import com.mahjong.server.mina.protocol.MessageProtocol;
import com.mahjong.server.model.Player;

/**
 * 房间列表
 * 
 * @author Simple
 * @date 2013-3-5 上午10:59:26
 * @Description
 */
@Component
@Scope("prototype")
public class PlayerInfoReq extends MessageProtocol {

	private static final byte TAG = Common.Req;

	private static final short PROTOCOL_NUM = 0x0013;

	private Logger log = Logger.getLogger(this.getClass());

	private int id;

	@Override
	public short getProtocolNum() {
		return PROTOCOL_NUM;
	}

	@Override
	public byte getTag() {
		return TAG;
	}

	@Override
	public AbsMessageProtocol execute(IoSession session, AbsMessageProtocol req) {
		log.debug(this.getClass().getSimpleName() + " execute");
		Player player;
		if (id == 0) {
			player = onlineManager.getPlayerBySession(session);// 玩家自身
		} else {
			player = onlineManager.getPlayerById(id);
		}
		if (null != player) {
			return new PlayerInfoResp(Common.SUCCESS, player);
		}
		return new PlayerInfoResp(Common.FAILD, null);
	}

	@Override
	public void reqDecode(DataBuf buf) {
		log.debug(this.getClass().getSimpleName() + " decode");
		id = buf.getInt();
	}
}
